<?php
	
	function checkJail()
	{
		$cid = $_SESSION['cid'];
		$time = time();

		$q = mysql_query("SELECT * FROM jail_log WHERE cid='$cid' AND endtime > '$time' AND finished ='0'");
		if(mysql_num_rows($q) == 1)
		{
			alert("You are in jail.");
			redirect("?page=jail");
		}

	}

	function setJail($jid, $minutes)
	{
		$cid = $_SESSION['cid'];
		$time = $minutes*60;
		$time = time()+$time;

		mysql_query("INSERT INTO jail_log (cid, jid, endtime) VALUES ('$cid', '$jid', '$time')");
		mysql_query("INSERT INTO jail_attempts (cid, jid, attempt, endtime) VALUES ('$cid', '$jid', '0', '$time')");
	}

	function getJailMessage()
	{
		$cid = $_SESSION['cid'];
		$time = time();

		$q = mysql_query("SELECT * FROM jail_log WHERE cid='$cid' AND endtime > '$time'");
		$f = mysql_fetch_array($q);

		$q2 = mysql_query("SELECT * FROM jails WHERE id='".$f['jid']."'");
		$f2 = mysql_fetch_array($q2);

		if(mysql_num_rows($q) > 0)
		{
			echo '<h3>You are in jail!</b></h3>
			<div id="starttime">'.$f['endtime'].'</div>
			You have <div id="timeleft"></div> of time left.
			<br><br>
			<b>Info:</b>
			<table>
				<tr>
					<td width="120"><b>Jail name</b></td><td width="150"><b>Break out chance</b></td><td width="110"><b>Payout bill</b></td><td width="120"><b>Energy Cost</b></td><td></td><td></td>
				</tr>
				<tr>
					<td>'.$f2['jailname'].'</td><td>'.$f2['breakoutchance'].'%</td><td>&#x2766;'.$f2['payout'].'</td><td>'.$f2['energy'].'</td><td><a href="?breakout=true"><button>Break Out</button></a></td><td><a href="?payout=true"><button>Pay Bill</button></a></td>
				</tr
			</table>';
		}
		else
		{
			echo '<h3>You are not in jail.</h3>';
		}
		
	}

	function breakOut()
	{
		$cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];

		$time = time();

		$q = mysql_query("SELECT * FROM jail_log WHERE cid='$cid' AND endtime > '$time'");
		$f = mysql_fetch_array($q);

		$q2 = mysql_query("SELECT * FROM jails WHERE id='".$f['jid']."'");
		$f2 = mysql_fetch_array($q2);

		$chance = $f2['breakoutchance'];
		$minutes = $f2['min-minutes'];
		$level = getCharLevel(getAidFromCid($cid));

		$chance = $chance+(round(($chance/100)*$level)+$level);

		$currenergy = getCharCurrEnergy($aid);
		$energycost = $f2['energy'];

		if($currenergy >= $energycost)
		{
			$q3 = mysql_query("SELECT * FROM jail_attempts WHERE cid='".$cid."' AND jid='".$f['jid']."' AND endtime = '".$f['endtime']."' AND attempt='0'");
			if(mysql_num_rows($q3) == 1)
			{
				if($random <= $chance)
				{
					mysql_query("UPDATE jail_log SET endtime='0', finished='1' WHERE cid='$cid' AND endtime > '$time'");
					mysql_query("UPDATE jail_attempts SET attempt='1'");
					alert("You successfully broke out of jail!");
					redirect('?page=profile');
				}
				else
				{
					$minutes = ceil($minutes/2);
					$addmin = $minutes*60;
					$newtime = $f['endtime']+$addmin;
					mysql_query("UPDATE jail_log SET endtime='".$newtime."' WHERE cid='".$cid."' AND endtime='".$f['endtime']."'");
					mysql_query("UPDATE jail_attempts SET endtime='".$newtime."', attempt='1' WHERE cid='".$cid."' AND endtime='".$f['endtime']."'");
					alert("You failed to break out, Your sit time has been extendend with $minutes minutes.");
					redirect('?page=jail');
				}
			}
			else if(mysql_num_rows($q) == 1 && mysql_num_rows($q3) == 0)
			{
				alert("You already tried to break out of this jail and failed.");
				redirect('?page=jail');
			}

			RemAddEnergy($energycost, 'rem');
		}
		else
		{
			alert("You don't have enough energy to break out.");
			redirect('?page=jail');
		}
		
	}

	function PayBill()
	{
		$cid = $_SESSION['cid'];
		$time = time();

		$aid = $_SESSION['uid'];

		$pollars = getPollars($aid);
		
		$q = mysql_query("SELECT * FROM jail_log WHERE cid='$cid' AND endtime > '$time'");
		$f = mysql_fetch_array($q);

		$q2 = mysql_query("SELECT * FROM jails WHERE id='".$f['jid']."'");
		$f2 = mysql_fetch_array($q2);

		$bill = $f2['payout'];

		if($pollars >= $bill)
		{
			$newpollars = $pollars-$bill;
			mysql_query("UPDATE account SET pollars='$newpollars' WHERE id='$aid'");
			mysql_query("UPDATE jail_log SET endtime='0', finished='1' WHERE cid='$cid' AND endtime > '$time'");
			alert("You bought yourself out of the jail for $bill Pollars!");
			redirect("?page=profile");
		}
		else
		{
			alert("You don't have enough Pollars!");
			redirect('?page=jail');
		}
	}

	if(isset($_GET['breakout']))
	{
		breakOut();
	}

	if(isset($_GET['payout']))
	{
		PayBill();
	}
?>